/**
 * jQuery newDOMElement plugin allow create DOM Elements "on-fly"
 * 
 */

jQuery.fn.newDOMElement = function(template){
    return this.each(function() {
        for (element in template) {
            var el = (typeof(template[element].tagName) === 'string' ) ?
                document.createElement(template[element].tagName) :
                document.createTextNode('');
            delete template[element].tagName;
            for (attr in template[element]){
                if (attr == 'className'){
                    jQuery(el).addClass(template[element][attr]);
                    delete template[element].className;
                }
                switch (typeof (template[element][attr])){
                    case 'string':
                        if (typeof(el[attr]) === 'string'){
                            el[attr] = template[element][attr];
                        } else {
                            el.setAttribute(attr, template[element][attr]);
                        }
                        break;
                    case 'function':
                        el[attr] = template[element][attr];
                        break;
                    case 'object':
                        if (attr === 'childNodes') jQuery(el).newDOMElement(template[element][attr]);
                        break;
                }
            }
            this.appendChild(el);
        }
    });
};
